package com.neusoft.controller;

import com.neusoft.feign.FoodFeignClient;
import com.neusoft.po.Business;
import com.neusoft.po.BusinessAdmin;
import com.neusoft.po.CommonResult;
import com.neusoft.service.BusinessService;
import com.neusoft.service.ClientWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RefreshScope //开启动态刷新
//@CrossOrigin("*")
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private ClientWalletService clientWalletService;
    @Qualifier("com.neusoft.feign.FoodFeignClient")
    @Autowired
    private FoodFeignClient foodFeignClient;

    @GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId)
            throws Exception {
        List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
        return new CommonResult(200, "success", list);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception {
        Business business = businessService.getBusinessById(businessId);
        // 在商家微服务中调用食品微服务
        CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
        // 如果食品微服务返回降级响应，那么就返回空集合
        if (result.getCode() == 200) {
            business.setFoodList(result.getResult());
        } else {
            business.setFoodList(new ArrayList());
        }
        return new CommonResult(200, "success", business);
    }

    @PostMapping("/saveBusiness/{businessId}/{businessName}")
    public CommonResult saveUser(@PathVariable("businessId") String userId,
                                          @PathVariable("businessName") String userName) throws Exception {
        Business param = new Business();
        param.setBusinessId(Integer.parseInt(userId));
        param.setBusinessName(userName);
        clientWalletService.initializeWallet(Long.valueOf(param.getBusinessId()));
        int result = businessService.saveBusiness(param);
        return new CommonResult(200, "success", result);
    }

}